import { useEffect, useState } from 'react';
import { Message, Input, Form, Box, Button, Card, DatePicker, Radio, Upload, Table, Rating } from '@alifd/next';

export const TimerUpdate = (props: {
    onEndTime: any
}): JSX.Element => {
    const [startTime, setStartTime] = useState(new Date().getTime());
    const [nowTime, setNowTime] = useState(new Date().getTime());
    useEffect(() => {
        let focusFunc: any;
        const timeFun = () => {
            setNowTime(() => {
                let ntime = new Date().getTime()
                if (ntime - startTime > 60 * 7 * 1000) {
                    // 这个时候已经
                    window.removeEventListener('focus', focusFunc);
                    clearInterval(timer);
                    props.onEndTime && props.onEndTime()
                }
                return ntime;
            });
        }
        let timer = setInterval(timeFun, 1000);
        window.addEventListener('blur', () => {
            clearInterval(timer);
        });
        window.addEventListener('focus', focusFunc = () => {
            clearInterval(timer);
            setStartTime(new Date().getTime());
            timer = setInterval(timeFun, 1000);
        });
    }, []);
    return <span>
        {`已经计划时间${(nowTime - startTime) / 1000}`}
    </span>;
};

export default TimerUpdate;
